package com.it2297.controller;

import java.awt.Color;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;

import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.sql.Date;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;

import javax.imageio.ImageIO;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSpinner;
import javax.swing.JSpinner.DefaultEditor;
import javax.swing.JTable;
import javax.swing.SpinnerNumberModel;
import javax.swing.SwingConstants;
import javax.swing.border.BevelBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.general.DefaultPieDataset;

import com.alee.extended.image.WebDecoratedImage;
import com.it2297.component.ImageChooser;
import com.it2297.component.ImageCropper;
import com.it2297.main.UserApp;
import com.it2297.model.Comment;
import com.it2297.model.Order;
import com.it2297.model.OrderItem;
import com.it2297.model.Order_Full_View;
import com.it2297.model.Payment;
import com.it2297.model.Product;
import com.it2297.model.UsedItem;
import com.it2297.model.User;
import com.it2297.model.salePieChart;

import com.it2297.renderer.commentCellRenderer;
import com.it2297.util.Email;
import com.it2297.util.ImageProcessor;
import com.it2297.view.CreateCommentDialog;
import com.it2297.view.CreateProductView;
import com.it2297.view.CustomerOrderListDialog;

import com.it2297.view.NewShopView;
import com.it2297.view.OrderEmailSender;
import com.it2297.view.OrderHistory;
import com.it2297.view.OrderPaid;
import com.it2297.view.PaymentView;
import com.it2297.view.PieChartView;
import com.it2297.view.ProductCommentView;
import com.it2297.view.ProductDetailDialog;
import com.it2297.view.RetrieveProductView;
import com.it2297.view.ShoppingCartView;
import com.it2297.view.UpdatedProductDialog;

public class ProductController {
	private OrderEmailSender ordermail;
	private CreateProductView view;
	private ProductDetailDialog pdd;
	private final ArrayList<BufferedImage> bimgs = new ArrayList<BufferedImage>();
	private BufferedImage temp_img;
	private UpdatedProductDialog updatedDialog;
	private ProductDetailDialog DetailDialog;
	private ProductCommentView commentView;
	private Product selectedProduct;
	private CreateCommentDialog CreateCom;
	private ShoppingCartView cartView;
	private NewShopView esview;
	private OrderPaid oldOrder;
	private OrderHistory orderhistory;
	private Object[] selectedItems;
	private PaymentView payView;
	private RetrieveProductView retireveView;
	private CustomerOrderListDialog list;
	private java.util.Date date = new java.util.Date();
	private User user;
	private PieChartView pieChart;
	private ShoppingCartView shopCart;
	private static double total_Price;
	private static ArrayList<Product> order_list = new ArrayList<Product>();
	private static ArrayList<Integer> quantity_item = new ArrayList<Integer>();
	private static ArrayList<Payment> pay = new ArrayList<Payment>();
	private static ArrayList<Order> orderList = new ArrayList<Order>();
	private static ArrayList<OrderItem> order_item = new ArrayList<OrderItem>();
	private static ArrayList<Product> Product_ordered = new ArrayList<Product>();
	private static ArrayList<Order_Full_View> oldOrderList = new ArrayList<Order_Full_View>();
	public static final JPanel panel = new JPanel();

	// constructor
	public ProductController() {

	}

	public ProductController(PieChartView pie) {
		this.pieChart = pie;
	}

	public ProductController(OrderEmailSender sender) {
		this.ordermail = sender;
	}

	public ProductController(RetrieveProductView view) {
		this.retireveView = view;
	}

	public ProductController(PaymentView view) {
		this.payView = view;
	}

	public ProductController(ShoppingCartView cartView) {
		this.shopCart = cartView;
	}

	public ProductController(CreateCommentDialog view) {
		this.CreateCom = view;
	}

	public ProductController(ProductDetailDialog detail) {
		this.DetailDialog = detail;
	}

	public ProductController(CreateProductView cpv) {
		this.view = cpv;
	}

	public ProductController(NewShopView eShopView) {
		this.esview = eShopView;
	}

	public ProductController(UpdatedProductDialog upd) {
		this.updatedDialog = upd;
	}

	public ProductController(ProductCommentView view) {
		this.commentView = view;
	}

	public ProductController(CustomerOrderListDialog orderlist) {
		this.list = orderlist;
	}

	public ProductController(OrderHistory orderHistory) {
		this.orderhistory = orderHistory;
	}

	public ProductController(OrderPaid orderPaid) {
		this.oldOrder = orderPaid;
	}

	// getter and setter
	public static ArrayList<Order_Full_View> getOldOrderList() {
		return oldOrderList;
	}

	public static void setOldOrderList(ArrayList<Order_Full_View> oldOrderList) {
		ProductController.oldOrderList.addAll(oldOrderList);
	}

	public void addOrderList(Product product) {
		order_list.add(product);
	}

	public ArrayList<Product> getOrderList() {
		return order_list;
	}

	public void setTotalPrice(double price) {
		total_Price = price;
	}

	public double getTotalPrice() {
		return total_Price;
	}

	public ArrayList<Integer> getQuantity() {
		return quantity_item;
	}

	public void setPaymentList(ArrayList<Payment> pay) {
		this.pay.addAll(pay);
	}

	public void setPayment(Payment payment) {
		pay.add(payment);
	}

	public ArrayList<Payment> getPayment() {
		return pay;
	}

	public void setOrderList(ArrayList<Order> arrayList) {
		this.orderList.addAll(arrayList);
	}

	public ArrayList<Order> getOrderListPayment() {
		return orderList;
	}

	public Payment getPaymentItem(int id) {
		Payment payment = new Payment();
		return payment.getPayment(id);
	}

	public Payment getPCPaymentHistory(int id) {
		Payment payment = new Payment();
		return payment.getPaymentHistory(id);
	}

	public ArrayList<Order> getOrderDetail(int orderID) {
		Order order = new Order();
		return order.getAllPaymentDetail(orderID);

	}

	public ArrayList<Order> getOrderHistory(String nric) {
		Order order = new Order();
		return order.getAllPaymentHistory(nric);

	}

	public ArrayList<OrderItem> getOrder_item() {
		return order_item;
	}

	public void setOrder_item(ArrayList<OrderItem> order_item) {
		ProductController.order_item.removeAll(this.order_item);
		this.order_item.addAll(order_item);
	}

	public ArrayList<Product> getProduct_ordered() {
		return Product_ordered;
	}

	public void setProduct_ordered(Product product) {
		this.Product_ordered.add(product);
	}

	// method
	public boolean setUnpaid() {
		boolean valid = false;
		if (!getPayment().isEmpty()) {
			ProductController.pay.removeAll(pay);
		}
		Payment payment = new Payment();
		ArrayList<Payment> pay =payment.getAllPayment();
		if (pay.isEmpty()) {
			valid = false;
		} else {
			setPaymentList(pay);
			Order order = new Order();
			for (int i = 0; i < getPayment().size(); i++) {
				setOrderList(order.getUnpaid(getPayment().get(i)
						.getPayment_id()));
			}

			OrderItem Item = new OrderItem();
			for (int i = 0; i < getOrderList().size(); i++) {
				setOrder_item(Item.getSelectedItem(getPayment().get(i).getPayment_id()));
			}

			Product product = new Product();
			for (int i = 0; i < getOrder_item().size(); i++) {
				setProduct_ordered(product.getunpaidProduct(getOrder_item()
						.get(i).getProdID()));
			}
			valid = true;
		}
		return valid;
	}

	public boolean setOrderlistItem(int OrderID) {
		boolean valid = false;
		if (OrderID < 0) {

			valid = false;
		} else {
			setOrderList(getOrderDetail(OrderID));

			// get payment details
			for (int i = 0; i < getOrderListPayment().size(); i++) {
				setPayment(getPaymentItem(getOrderListPayment().get(i).getPaymentid()));
			}
			valid = true;
		}
		return valid;
	}

	public void setOrderlistHistory(String nric) {
		setOrderList(getOrderHistory(nric));

		// get payment details
		for (int i = 0; i < getOrderListPayment().size(); i++) {
			setPayment(getPCPaymentHistory(getOrderListPayment().get(i)
					.getPaymentid()));
		}
	}

	public void setAllPaid(String nric) {
		Order_Full_View order = new Order_Full_View();
		setOldOrderList(order.getAllPaid(nric));
	}

	public ArrayList<Order> getOrderedHistory(String nric) {
		Order order = new Order();
		return order.getOrder(nric);
	}

	public Product getSelectProduct(int id) {
		Product product = new Product();
		return product.getSelected(id);
	}

	public ArrayList<OrderItem> getItem(int id) {
		OrderItem item = new OrderItem();
		return item.getSelectedItem(id);
	}

	public int CreateOrderList(int n, String nric) {
		Order order = new Order();

		return order.CreateOrder(n, nric, new Timestamp(date.getTime()));
	}

	public void CreateOrderItem(int quantity, int productID, int orderid) {
		OrderItem item = new OrderItem();
		item.setOrderID(orderid);
		item.setProdID(productID);
		item.setQuantity(quantity);
		item.CreateOrderItem(item);
	}

	public boolean CreateProduct() {
		boolean successfull = false;
		Product product = new Product();
		boolean exist = product.isObsProduct(view.getProductName());
		if (!exist) {
			boolean valid = product.isExistProduct(view.getProductName());
			if (valid) {
				successfull = false;
				JOptionPane.showMessageDialog(null, "Product exist!");
			} else {
				product.setProdName(view.getProductName());
				product.setDesc(view.getProductDesc());
				product.setPrice(view.getProductPrice());
				product.setStock(view.getProductQuantity());
				product.setStatus("ava");
				product.setCategory(view.getCategory());

				product.addProduct(product);
				successfull = true;
				JOptionPane.showMessageDialog(null, "Successfull Created!");
			}
		} else {
			successfull = false;
			JOptionPane.showMessageDialog(null, "Product exist But obsolete!");
		}
		return successfull;
	}

	public void CreateComment() {

		Comment comment = new Comment();

		comment.setTitle(CreateCom.getTitle());
		// comment.setUserId(user.getNric());
		comment.setUserId(CreateCom.getUserid());
		comment.setMessage(CreateCom.getComment());
		comment.setDateCreate(new Timestamp(date.getTime()));
		comment.setDateUpdate(new Timestamp(date.getTime()));

		comment.CreateComment(comment);
	}

	public void showCreateComment(String title) {
		CreateCommentDialog commentDialog = new CreateCommentDialog();
		commentDialog.setTitle(title);
		commentDialog.setVisible(true);
	}

	public void ShowCreateProduct() {
		CreateProductView CreateView = new CreateProductView();
		CreateView.setVisible(true);
	}

	public boolean UpdatedProduct() {
		Product product = new Product();
		product.setProdName(updatedDialog.getProductName());
		product.setDesc(updatedDialog.getProductDesc());
		product.setPrice(updatedDialog.getProductPrice());
		product.setStock(updatedDialog.getProductQuantity());
		product.setStatus("ava");
		product.setCategory(updatedDialog.getProductType());
		return product.updateProduct(product);
	}

	public void showCreateProduct() {
		CreateProductView cpView = new CreateProductView();
		cpView.setVisible(true);
	}

	public int getQuantity(int productid) {
		Product product = new Product();
		return product.getQuantity(productid);
	}

	// updated quantity function
	public void updatedQuantityCart(int quantity, int productid) {
		Product product = new Product();
		product.updatedQuantity(quantity, productid);
	}

	// Create and show pie chart
	public void showPieChart(int month) {
		panel.removeAll();
		salePieChart pieChart = new salePieChart();
		JButton btnSend = new JButton("Send");
		btnSend.setBounds(500, 0, 100, 100);
		DefaultPieDataset pieDataset = new DefaultPieDataset();

		for (int i = 0; i < pieChart.getPieChart(month).size(); i++) {
			pieDataset.setValue(pieChart.getPieChart(month).get(i)
					.getProdname(), pieChart.getPieChart(month).get(i)
					.getQuantity());
		}

		JFreeChart chart = ChartFactory.createPieChart("Sales Pie Chart",
				pieDataset, false, true, false);

		ChartPanel chartFrame = new ChartPanel(chart);
		panel.add(chartFrame);
		PieChartView pieView = new PieChartView();
		pieView.getScrollpane().getViewport().removeAll();
		pieView.setPanel(panel);
		pieView.setVisible(true);

		chartFrame.setSize(500, 500);
	}

	// show product full detail dialog
	public void showProductDetail() {
		selectedProduct = (Product) esview.getSelected();
		DetailDialog = new ProductDetailDialog();
		commentView = new ProductCommentView();
		DetailDialog.setProductID(selectedProduct.getProductid());
		DetailDialog.setProductName(selectedProduct.getProdName());
		DetailDialog.setProductDesc(selectedProduct.getDesc());
		DetailDialog.setProductPrice(selectedProduct.getPrice());
		DetailDialog.setProductQuantity(selectedProduct.getStock());
		DetailDialog.setCategory(selectedProduct.getCategory());
		DetailDialog.setImg(selectedProduct.getProdName());
		commentView.setProductName(selectedProduct.getProdName());
		commentView.refresh();
		DetailDialog.setVisible(true);
	}

	// Email sender
	public void EmailSender(String to, int id, String msg, String status) {
		ordermail = new OrderEmailSender();
		ordermail.setToEmail(to);
		ordermail.setOrderID("Order id: " + Integer.toString(id) + " Status: "
				+ status);
		ordermail.setMessage(msg);
		ordermail.setVisible(true);

	}

	// show deleted product full detail dialog
	public void showDelProductDetail() {
		selectedProduct = (Product) retireveView.getSelected();
		DetailDialog = new ProductDetailDialog();
		DetailDialog.setProductID(selectedProduct.getProductid());
		DetailDialog.setProductName(selectedProduct.getProdName());
		DetailDialog.setProductDesc(selectedProduct.getDesc());
		DetailDialog.setProductPrice(selectedProduct.getPrice());
		DetailDialog.setProductQuantity(selectedProduct.getStock());
		DetailDialog.setCategory(selectedProduct.getCategory());
		DetailDialog.setImg(selectedProduct.getProdName());
		DetailDialog.setBtn();
		DetailDialog.setVisible(true);
	}

	// show order detail

	// show updated dialog
	public void showProductDetailUpdated() {
		selectedProduct = (Product) esview.getSelected();
		updatedDialog = new UpdatedProductDialog();

		updatedDialog.setProductName(selectedProduct.getProdName());
		updatedDialog.setProductDesc(selectedProduct.getDesc());
		updatedDialog.setProductPrice(selectedProduct.getPrice());
		updatedDialog.setProductQuantity(selectedProduct.getStock());
		updatedDialog.setImg(selectedProduct.getProdName());
		updatedDialog.setProductType(selectedProduct.getCategory());

		updatedDialog.setVisible(true);
	}

	// image chooser
	public ImageIcon showImageChooser() {
		ImageChooser imgChooser = new ImageChooser();
		imgChooser.setMultiSelectionEnabled(false);
		imgChooser.showDialog();
		ImageIcon imgIcon = null;
		temp_img = imgChooser.getSelectedImage();

		if (temp_img != null) {
			temp_img = ImageProcessor.resize(temp_img, 158, 120, true, true);
			imgIcon = new ImageIcon(temp_img);

			WebDecoratedImage wdimg = new WebDecoratedImage(imgIcon);
			wdimg.addMouseListener(new MouseAdapter() {

				@Override
				public void mouseClicked(MouseEvent e) {
					if (e.getClickCount() > 1) {
						ImageCropper dt = new ImageCropper(bimgs.get(0));
						dt.setVisible(true);
					}
				}
			});

		} else {
			System.out.println("nothing");
		}

		return imgIcon;
	}

	// image savinginto folder
	public boolean saveImages() {
		boolean valid = false;
		try {
			if (temp_img == null) {
				JOptionPane.showMessageDialog(null, "Please select a Image");
				valid = false;
			} else {
				ImageIO.write(temp_img, "png", new File("img/product-img/"
						+ view.getProductName() + ".png"));
				valid = true;
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return valid;
	}

	// save image function
	public void saveImagesUpdated() {

		try {
			if (temp_img == null) {

			} else {
				ImageIO.write(temp_img, "png", new File("img/product-img/"
						+ updatedDialog.getProductName() + ".png"));
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	// get all paid order
	public void addOldOrder() {
		OrderPaid.getScrollpane().getViewport().removeAll();
		JLabel lblorderid = new JLabel("Order Id");
		JLabel lblDate = new JLabel("Payment Date");
		JLabel lblprice = new JLabel("Total Price");
		JLabel lblView = new JLabel("View Item");
		final JPanel mainpanel = new JPanel();
		mainpanel.setPreferredSize(new Dimension(750, getOldOrderList().size() * 200 + 50));
		FlowLayout layout = (FlowLayout) mainpanel.getLayout();
		layout.setAlignment(FlowLayout.LEFT);
		mainpanel.add(lblorderid);
		mainpanel.add(lblDate);
		mainpanel.add(lblprice);
		mainpanel.add(lblView);

		for (int i = 0; i<getOldOrderList().size(); i++) {
			JPanel pane = new JPanel();
			final int temp = i;
			pane.setLayout(null);
			if (i % 2 == 0) {
				pane.setBackground(Color.WHITE);
			} else {
				pane.setBackground(new Color(240, 240, 240));
			}
			pane.setPreferredSize(new Dimension(750, 90));
			JLabel order_id = new JLabel();
			order_id.setSize(new Dimension(90, 100));
			order_id.setBounds(30, 3, 150, 100);
			order_id.setFont(new Font("Tahoma", Font.PLAIN, 18));
			JLabel order_date = new JLabel();
			order_date.setSize(new Dimension(90, 100));
			order_date.setBounds(140, 3, 250, 100);
			order_date.setFont(new Font("Tahoma", Font.PLAIN, 18));
			JLabel order_price = new JLabel();
			order_price.setSize(new Dimension(90, 100));
			order_price.setBounds(400, 3, 180, 100);
			order_price.setFont(new Font("Tahoma", Font.PLAIN, 18));
			JButton btn_select = new JButton("view Item");
			btn_select.setSize(new Dimension(70, 30));
			btn_select.setBounds(530, 35, 150, 30);

			order_id.setText(Integer.toString(getOldOrderList().get(i)
					.getOrderId()));

			String date = String.format("%1$TD, %1$TT", getOldOrderList().get(i).getPayDate());
			order_date.setText(date);
			order_price.setText(Double.toString(getOldOrderList().get(i).getTotal()));
	
			btn_select.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					setOrder_item( getItem(getOldOrderList().get(temp).getOrderId()));
					ProductController.Product_ordered.removeAll(Product_ordered);
					for (int i = 0; i < getOrder_item().size(); i++) {
						setProduct_ordered(getSelectProduct(getOrder_item().get(i).getProdID()));
					}

					addOrderItem();
					CustomerOrderListDialog order = new CustomerOrderListDialog();
					order.setVisible(true);
				}

			});
			pane.add(order_id);
			pane.add(order_date);
			pane.add(order_price);
			pane.add(btn_select);
			mainpanel.add(pane);

		}
		lblorderid.setVerticalAlignment(SwingConstants.BOTTOM);
		lblorderid.setPreferredSize(new Dimension(90, 20));
		lblorderid.setOpaque(true);
		lblorderid.setHorizontalAlignment(SwingConstants.CENTER);
		lblorderid.setForeground(Color.WHITE);
		lblorderid.setBorder(new BevelBorder(BevelBorder.LOWERED, new Color(
				128, 128, 128), new Color(128, 128, 128), new Color(192, 192,
				192), new Color(192, 192, 192)));
		lblorderid.setBackground(Color.GRAY);

		lblDate.setVerticalAlignment(SwingConstants.BOTTOM);
		lblDate.setPreferredSize(new Dimension(250, 20));
		lblDate.setOpaque(true);
		lblDate.setHorizontalAlignment(SwingConstants.CENTER);
		lblDate.setForeground(Color.WHITE);
		lblDate.setBorder(new BevelBorder(BevelBorder.LOWERED, new Color(128,
				128, 128), new Color(128, 128, 128), new Color(192, 192, 192),
				new Color(192, 192, 192)));
		lblDate.setBackground(Color.GRAY);

		lblprice.setVerticalAlignment(SwingConstants.BOTTOM);
		lblprice.setPreferredSize(new Dimension(130, 20));
		lblprice.setOpaque(true);
		lblprice.setHorizontalAlignment(SwingConstants.CENTER);
		lblprice.setForeground(Color.WHITE);
		lblprice.setBorder(new BevelBorder(BevelBorder.LOWERED, new Color(128,
				128, 128), new Color(128, 128, 128), new Color(192, 192, 192),
				new Color(192, 192, 192)));
		lblprice.setBackground(Color.GRAY);

		lblView.setVerticalAlignment(SwingConstants.BOTTOM);
		lblView.setPreferredSize(new Dimension(240, 20));
		lblView.setOpaque(true);
		lblView.setHorizontalAlignment(SwingConstants.CENTER);
		lblView.setForeground(Color.WHITE);
		lblView.setBorder(new BevelBorder(BevelBorder.LOWERED, new Color(128,
				128, 128), new Color(128, 128, 128), new Color(192, 192, 192),
				new Color(192, 192, 192)));
		lblView.setBackground(Color.GRAY);

		OrderPaid.setPanel(mainpanel);
		
	}

	// cutsomer payment history
	public void addOrderHistory() {
		OrderHistory.getScrollpane().getViewport().removeAll();
		JLabel lblorderid = new JLabel("Order Id");
		JLabel lblDate = new JLabel("Order Date");
		JLabel lblprice = new JLabel("Total Price");
		JLabel label = new JLabel("status");
		JLabel lblPaid = new JLabel("View");
		JLabel lblSend = new JLabel("Email");
		JLabel lblRemove = new JLabel("Remove");
		final JPanel mainpanel = new JPanel();
		mainpanel.setPreferredSize(new Dimension(750, getOrderListPayment()
				.size() * 200 + 50));
		FlowLayout layout = (FlowLayout) mainpanel.getLayout();
		layout.setAlignment(FlowLayout.LEFT);
		mainpanel.add(lblorderid);
		mainpanel.add(lblDate);
		mainpanel.add(lblprice);
		mainpanel.add(label);
		mainpanel.add(lblPaid);
		mainpanel.add(lblSend);
		mainpanel.add(lblRemove);

		for (int i = 0; i < getOrderListPayment().size(); i++) {
			JPanel panel = new JPanel();
			final int temp = i;
			panel.setLayout(null);
			if (i % 2 == 0) {
				panel.setBackground(Color.WHITE);
			} else {
				panel.setBackground(new Color(240, 240, 240));
			}
			panel.setPreferredSize(new Dimension(750, 90));

			// JButton btn_detail = new JButton("More detail");
			JLabel order_id = new JLabel();
			order_id.setSize(new Dimension(90, 100));
			order_id.setBounds(30, 3, 90, 100);
			JLabel order_date = new JLabel();
			order_date.setSize(new Dimension(90, 100));
			order_date.setBounds(100, 3, 90, 100);
			JLabel order_price = new JLabel();
			order_price.setSize(new Dimension(90, 100));
			order_price.setBounds(200, 3, 90, 100);
			JButton btn_select = new JButton("view");
			btn_select.setSize(new Dimension(70, 30));
			btn_select.setBounds(390, 20, 70, 30);
			JLabel lblStatus = new JLabel();
			lblStatus.setSize(new Dimension(90, 100));
			lblStatus.setBounds(315, 20, 90, 50);
			lblStatus.setText(getPayment().get(i).getStatus());
			lblStatus.setFont(new Font("Tahoma", Font.PLAIN, 18));
			JButton btn_send = new JButton("Email");
			btn_send.setBounds(490, 20, 70, 30);
			JButton btn_remove = new JButton("Remove");
			btn_remove.setBounds(570, 20, 90, 30);
			final int id = getOrderListPayment().get(temp).getOrderID();
			btn_remove.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					
					Order order = new Order();
					OrderItem item = new OrderItem();
					Payment payment = new Payment();
					item.deleteItem(id);
					order.deleteOrder(id);
					payment.DeletePayment(getOrderListPayment().get(temp).getPaymentid());
					ProductController.orderList.removeAll(orderList);
					OrderHistory.getScrollpane().getViewport().removeAll();
					setOrderlistHistory(UserApp.getCurrentUser().getNric());
					addOrderHistory();
				}
			});
			btn_select.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					setOrder_item(getItem(id));
					ProductController.Product_ordered.removeAll(Product_ordered);
					for (int i = 0; i < getOrder_item().size(); i++) {
						setProduct_ordered(getSelectProduct(getOrder_item().get(i).getProdID()));
					}

					addOrderItem();
					CustomerOrderListDialog order = new CustomerOrderListDialog();
					order.setVisible(true);
				}

			});

			btn_send.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					String msg = "Name" + (padLeft(" ", 5) + " ")
							+ "Price per item " + " \n";
					setOrder_item(getItem(getOrderListPayment().get(temp)
							.getOrderID()));

					for (int i = 0; i < getOrder_item().size(); i++) {
						setProduct_ordered(getSelectProduct(getOrder_item()
								.get(i).getProdID()));
					}
					for (int i = 0; i < getOrder_item().size(); i++) {
						msg = msg
								+ getSelectProduct(
										getOrder_item().get(i).getProdID())
										.getProdName()
								+ (padLeft(" ", 5) + " ")
								+ getSelectProduct(
										getOrder_item().get(i).getProdID())
										.getPrice() + "\n";
					}

					EmailSender(UserApp.getCurrentUser().getEmail(),
							getOrderListPayment().get(temp).getOrderID(), msg,
							getPayment().get(temp).getStatus());
					// ProductController.Product_ordered.removeAll(Product_ordered);
					clear();
					setOrderlistHistory(UserApp.getCurrentUser().getNric());
					addOrderHistory();

					addProducts();
				}

			});

			order_id.setFont(new Font("Tahoma", Font.PLAIN, 18));
			order_date.setFont(new Font("Tahoma", Font.PLAIN, 18));
			order_price.setFont(new Font("Tahoma", Font.PLAIN, 18));
			order_id.setText(Integer.toString(getOrderListPayment().get(i)
					.getOrderID()));
			String s = String.format("%1$TD", getOrderListPayment().get(i)
					.getOrderDate());
			order_date.setText(s);
			order_price.setText(Double
					.toString(getPayment().get(i).getAmount()));
			panel.add(order_id);
			panel.add(order_date);
			panel.add(order_price);
			panel.add(lblStatus);
			panel.add(btn_select);
			panel.add(btn_send);
			panel.add(btn_remove);

			mainpanel.add(panel);
		}

		lblorderid.setVerticalAlignment(SwingConstants.BOTTOM);
		lblorderid.setPreferredSize(new Dimension(90, 20));
		lblorderid.setOpaque(true);
		lblorderid.setHorizontalAlignment(SwingConstants.CENTER);
		lblorderid.setForeground(Color.WHITE);
		lblorderid.setBorder(new BevelBorder(BevelBorder.LOWERED, new Color(
				128, 128, 128), new Color(128, 128, 128), new Color(192, 192,
				192), new Color(192, 192, 192)));
		lblorderid.setBackground(Color.GRAY);

		lblDate.setVerticalAlignment(SwingConstants.BOTTOM);
		lblDate.setPreferredSize(new Dimension(90, 20));
		lblDate.setOpaque(true);
		lblDate.setHorizontalAlignment(SwingConstants.CENTER);
		lblDate.setForeground(Color.WHITE);
		lblDate.setBorder(new BevelBorder(BevelBorder.LOWERED, new Color(128,
				128, 128), new Color(128, 128, 128), new Color(192, 192, 192),
				new Color(192, 192, 192)));
		lblDate.setBackground(Color.GRAY);

		lblprice.setVerticalAlignment(SwingConstants.BOTTOM);
		lblprice.setPreferredSize(new Dimension(90, 20));
		lblprice.setOpaque(true);
		lblprice.setHorizontalAlignment(SwingConstants.CENTER);
		lblprice.setForeground(Color.WHITE);
		lblprice.setBorder(new BevelBorder(BevelBorder.LOWERED, new Color(128,
				128, 128), new Color(128, 128, 128), new Color(192, 192, 192),
				new Color(192, 192, 192)));
		lblprice.setBackground(Color.GRAY);

		label.setVerticalAlignment(SwingConstants.BOTTOM);
		label.setPreferredSize(new Dimension(90, 20));
		label.setOpaque(true);
		label.setHorizontalAlignment(SwingConstants.CENTER);
		label.setForeground(Color.WHITE);
		label.setBorder(new BevelBorder(BevelBorder.LOWERED, new Color(128,
				128, 128), new Color(128, 128, 128), new Color(192, 192, 192),
				new Color(192, 192, 192)));
		label.setBackground(Color.GRAY);

		lblPaid.setVerticalAlignment(SwingConstants.BOTTOM);
		lblPaid.setPreferredSize(new Dimension(90, 20));
		lblPaid.setOpaque(true);
		lblPaid.setHorizontalAlignment(SwingConstants.CENTER);
		lblPaid.setForeground(Color.WHITE);
		lblPaid.setBorder(new BevelBorder(BevelBorder.LOWERED, new Color(128,
				128, 128), new Color(128, 128, 128), new Color(192, 192, 192),
				new Color(192, 192, 192)));
		lblPaid.setBackground(Color.GRAY);

		lblSend.setVerticalAlignment(SwingConstants.BOTTOM);
		lblSend.setPreferredSize(new Dimension(90, 20));
		lblSend.setOpaque(true);
		lblSend.setHorizontalAlignment(SwingConstants.CENTER);
		lblSend.setForeground(Color.WHITE);
		lblSend.setBorder(new BevelBorder(BevelBorder.LOWERED, new Color(128,
				128, 128), new Color(128, 128, 128), new Color(192, 192, 192),
				new Color(192, 192, 192)));
		lblSend.setBackground(Color.GRAY);

		lblRemove.setVerticalAlignment(SwingConstants.BOTTOM);
		lblRemove.setPreferredSize(new Dimension(90, 20));
		lblRemove.setOpaque(true);
		lblRemove.setHorizontalAlignment(SwingConstants.CENTER);
		lblRemove.setForeground(Color.WHITE);
		lblRemove.setBorder(new BevelBorder(BevelBorder.LOWERED, new Color(128,
				128, 128), new Color(128, 128, 128), new Color(192, 192, 192),
				new Color(192, 192, 192)));
		lblRemove.setBackground(Color.GRAY);
		OrderHistory.setPanel(mainpanel);
	}

	// payment list
	public void addOrders() {
		JLabel lblorderid = new JLabel("Order id");
		JLabel lblDate = new JLabel("Order date");
		JLabel lblprice = new JLabel("Total Price");
		JLabel label = new JLabel("View");
		JLabel lblPaid = new JLabel("Pay");
		final JPanel mainpanel = new JPanel();
		mainpanel.setPreferredSize(new Dimension(750, getOrderListPayment()
				.size() * 200));
		FlowLayout layout = (FlowLayout) mainpanel.getLayout();
		layout.setAlignment(FlowLayout.LEFT);
		mainpanel.add(lblorderid);
		mainpanel.add(lblDate);
		mainpanel.add(lblprice);
		mainpanel.add(label);
		mainpanel.add(lblPaid);

		for (int i = 0; i < getOrderListPayment().size(); i++) {
			JPanel panel = new JPanel();
			final int temp = i;
			panel.setLayout(null);
			if (i % 2 == 0) {
				panel.setBackground(Color.WHITE);
			} else {
				panel.setBackground(new Color(240, 240, 240));
			}
			panel.setPreferredSize(new Dimension(750, 90));

			// JButton btn_detail = new JButton("More detail");
			JLabel order_id = new JLabel();
			order_id.setSize(new Dimension(100, 100));
			order_id.setBounds(30, 3, 100, 100);
			JLabel order_date = new JLabel();
			order_date.setSize(new Dimension(100, 100));
			order_date.setBounds(130, 3, 100, 100);
			JLabel order_price = new JLabel();
			order_price.setSize(new Dimension(100, 100));
			order_price.setBounds(300, 3, 100, 100);
			JButton btn_select = new JButton("view Order");
			btn_select.setSize(new Dimension(100, 100));
			btn_select.setBounds(400, 20, 100, 50);
			JButton btn_pay = new JButton();
			btn_pay.setText("Pay");
			btn_pay.setBounds(550, 20, 100, 50);

			btn_select.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					setOrder_item(getItem(getOrderListPayment().get(temp)
							.getOrderID()));
					ProductController.Product_ordered
							.removeAll(Product_ordered);
					for (int i = 0; i < getOrder_item().size(); i++) {
						setProduct_ordered(getSelectProduct(getOrder_item()
								.get(i).getProdID()));
					}

					addOrderItem();
					CustomerOrderListDialog order = new CustomerOrderListDialog();
					order.setVisible(true);
				}

			});
			btn_pay.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					Payment payment = new Payment();
					String total = Double.toString(payment.getPayment(
							getOrderListPayment().get(temp).getPaymentid())
							.getAmount());
					int n = JOptionPane.showConfirmDialog(null, "Please Pay: S$"
							+ total, "Confirm Payment",
							JOptionPane.YES_NO_OPTION);
					if (n == JOptionPane.YES_OPTION) {
						boolean result = payment
								.makePayment(getOrderListPayment().get(temp)
										.getPaymentid());
						if (result) {
							ProductController.pay.removeAll(pay);
							ProductController.orderList.removeAll(orderList);

							setOrderlistItem(PaymentView.getID());
							addOrders();
							JOptionPane.showMessageDialog(null,
									"Payment done!!!");
						} else {
							JOptionPane.showMessageDialog(null,
									"Payment not found!!!!");
						}
					}
				}

			});

			order_id.setFont(new Font("Tahoma", Font.PLAIN, 18));
			order_date.setFont(new Font("Tahoma", Font.PLAIN, 18));
			order_price.setFont(new Font("Tahoma", Font.PLAIN, 18));
			order_id.setText(Integer.toString(getOrderListPayment().get(i)
					.getOrderID()));
			String s = String.format("%1$TD", getOrderListPayment().get(i)
					.getOrderDate());
			order_date.setText(s);
			order_price.setText(Double
					.toString(getPayment().get(i).getAmount()));
			panel.add(order_id);
			panel.add(order_date);
			panel.add(order_price);
			panel.add(btn_select);
			panel.add(btn_pay);

			mainpanel.add(panel);
		}

		lblorderid.setVerticalAlignment(SwingConstants.BOTTOM);
		lblorderid.setPreferredSize(new Dimension(100, 20));
		lblorderid.setOpaque(true);
		lblorderid.setHorizontalAlignment(SwingConstants.CENTER);
		lblorderid.setForeground(Color.WHITE);
		lblorderid.setBorder(new BevelBorder(BevelBorder.LOWERED, new Color(
				128, 128, 128), new Color(128, 128, 128), new Color(192, 192,
				192), new Color(192, 192, 192)));
		lblorderid.setBackground(Color.GRAY);

		lblDate.setVerticalAlignment(SwingConstants.BOTTOM);
		lblDate.setPreferredSize(new Dimension(150, 20));
		lblDate.setOpaque(true);
		lblDate.setHorizontalAlignment(SwingConstants.CENTER);
		lblDate.setForeground(Color.WHITE);
		lblDate.setBorder(new BevelBorder(BevelBorder.LOWERED, new Color(128,
				128, 128), new Color(128, 128, 128), new Color(192, 192, 192),
				new Color(192, 192, 192)));
		lblDate.setBackground(Color.GRAY);

		lblprice.setVerticalAlignment(SwingConstants.BOTTOM);
		lblprice.setPreferredSize(new Dimension(100, 20));
		lblprice.setOpaque(true);
		lblprice.setHorizontalAlignment(SwingConstants.CENTER);
		lblprice.setForeground(Color.WHITE);
		lblprice.setBorder(new BevelBorder(BevelBorder.LOWERED, new Color(128,
				128, 128), new Color(128, 128, 128), new Color(192, 192, 192),
				new Color(192, 192, 192)));
		lblprice.setBackground(Color.GRAY);

		label.setVerticalAlignment(SwingConstants.BOTTOM);
		label.setPreferredSize(new Dimension(150, 20));
		label.setOpaque(true);
		label.setHorizontalAlignment(SwingConstants.CENTER);
		label.setForeground(Color.WHITE);
		label.setBorder(new BevelBorder(BevelBorder.LOWERED, new Color(128,
				128, 128), new Color(128, 128, 128), new Color(192, 192, 192),
				new Color(192, 192, 192)));
		label.setBackground(Color.GRAY);

		lblPaid.setVerticalAlignment(SwingConstants.BOTTOM);
		lblPaid.setPreferredSize(new Dimension(150, 20));
		lblPaid.setOpaque(true);
		lblPaid.setHorizontalAlignment(SwingConstants.CENTER);
		lblPaid.setForeground(Color.WHITE);
		lblPaid.setBorder(new BevelBorder(BevelBorder.LOWERED, new Color(128,
				128, 128), new Color(128, 128, 128), new Color(192, 192, 192),
				new Color(192, 192, 192)));
		lblPaid.setBackground(Color.GRAY);
		PaymentView.setPanel(null);
		PaymentView.setPanel(mainpanel);
	}

	// display order item function
	public void addOrderItem() {
		JLabel lblIMG = new JLabel("Product Image");
		JLabel lblName = new JLabel("Product Name");
		JLabel lblQuantity = new JLabel("Quantity");
		JLabel lblPrice = new JLabel("Price");
		final JPanel mainpanel = new JPanel();
		mainpanel.setPreferredSize(new Dimension(600, getProduct_ordered()
				.size() * 100 + 50));
		FlowLayout layout = (FlowLayout) mainpanel.getLayout();
		layout.setAlignment(FlowLayout.LEFT);
		lblIMG.setVerticalAlignment(SwingConstants.BOTTOM);
		lblIMG.setHorizontalAlignment(SwingConstants.CENTER);
		lblIMG.setPreferredSize(new Dimension(110, 20));
		lblIMG.setBorder(new BevelBorder(BevelBorder.LOWERED, new Color(128,
				128, 128), new Color(128, 128, 128), new Color(192, 192, 192),
				new Color(192, 192, 192)));
		lblIMG.setOpaque(true);
		lblIMG.setBackground(Color.GRAY);
		lblIMG.setForeground(Color.white);
		mainpanel.add(lblIMG);
		mainpanel.add(lblName);
		mainpanel.add(lblQuantity);
		mainpanel.add(lblPrice);

		for (int i = 0; i < getProduct_ordered().size(); i++) {
			JPanel panel = new JPanel();
			panel.setLayout(null);
			panel.setPreferredSize(new Dimension(600, 90));
			if (i % 2 == 0) {
				panel.setBackground(Color.WHITE);
			} else {
				panel.setBackground(new Color(240, 240, 240));
			}
			Image bg = Toolkit
					.getDefaultToolkit()
					.createImage(
							System.getProperty("user.dir")
									+ "/img/product-img/"
									+ getProduct_ordered().get(i).getProdName()
									+ ".png")
					.getScaledInstance(100, 70, Image.SCALE_DEFAULT);
			JLabel lblimg = new JLabel();
			lblimg.setIcon(new ImageIcon(bg));
			lblimg.setBounds(10, 10, 100, 90);

			JLabel name = new JLabel();
			name.setText(getProduct_ordered().get(i).getProdName());
			name.setFont(new Font("Tahoma", Font.PLAIN, 18));
			name.setBounds(130, 10, 150, 50);

			JLabel quan = new JLabel();
			quan.setText(Integer.toString(getOrder_item().get(i).getQuantity()));
			quan.setFont(new Font("Tahoma", Font.PLAIN, 18));
			quan.setBounds(360, 10, 100, 50);

			JLabel price = new JLabel();
			double temp = getProduct_ordered().get(i).getPrice()
					* getOrder_item().get(i).getQuantity();
			price.setText(Double.toString(temp));
			price.setFont(new Font("Tahoma", Font.PLAIN, 18));
			price.setBounds(510, 10, 100, 50);
			panel.add(lblimg);
			panel.add(name);
			panel.add(quan);
			panel.add(price);
			mainpanel.add(panel);

		}

		lblIMG.setVerticalAlignment(SwingConstants.BOTTOM);
		lblIMG.setPreferredSize(new Dimension(100, 20));
		lblIMG.setOpaque(true);
		lblIMG.setHorizontalAlignment(SwingConstants.CENTER);
		lblIMG.setForeground(Color.WHITE);
		lblIMG.setBorder(new BevelBorder(BevelBorder.LOWERED, new Color(128,
				128, 128), new Color(128, 128, 128), new Color(192, 192, 192),
				new Color(192, 192, 192)));
		lblIMG.setBackground(Color.GRAY);

		lblName.setVerticalAlignment(SwingConstants.BOTTOM);
		lblName.setPreferredSize(new Dimension(180, 20));
		lblName.setOpaque(true);
		lblName.setHorizontalAlignment(SwingConstants.CENTER);
		lblName.setForeground(Color.WHITE);
		lblName.setBorder(new BevelBorder(BevelBorder.LOWERED, new Color(128,
				128, 128), new Color(128, 128, 128), new Color(192, 192, 192),
				new Color(192, 192, 192)));
		lblName.setBackground(Color.GRAY);

		lblQuantity.setVerticalAlignment(SwingConstants.BOTTOM);
		lblQuantity.setPreferredSize(new Dimension(150, 20));
		lblQuantity.setOpaque(true);
		lblQuantity.setHorizontalAlignment(SwingConstants.CENTER);
		lblQuantity.setForeground(Color.WHITE);
		lblQuantity.setBorder(new BevelBorder(BevelBorder.LOWERED, new Color(
				128, 128, 128), new Color(128, 128, 128), new Color(192, 192,
				192), new Color(192, 192, 192)));
		lblQuantity.setBackground(Color.GRAY);

		lblPrice.setVerticalAlignment(SwingConstants.BOTTOM);
		lblPrice.setPreferredSize(new Dimension(150, 20));
		lblPrice.setOpaque(true);
		lblPrice.setHorizontalAlignment(SwingConstants.CENTER);
		lblPrice.setForeground(Color.WHITE);
		lblPrice.setBorder(new BevelBorder(BevelBorder.LOWERED, new Color(128,
				128, 128), new Color(128, 128, 128), new Color(192, 192, 192),
				new Color(192, 192, 192)));
		lblPrice.setBackground(Color.GRAY);

		CustomerOrderListDialog.setPanel(mainpanel);

	}

	// shopping cart add product function
	public void addProducts() {
		System.gc();
		JLabel lblPrice_1 = new JLabel("Product Name");
		JLabel lblQuantity = new JLabel("Price");
		JLabel lblQuantity_1 = new JLabel("Quantity");
		JLabel label = new JLabel("");
		final JPanel mainpanel = new JPanel();
		mainpanel.setPreferredSize(new Dimension(750,
				getOrderList().size() * 100 + 50));
		FlowLayout layout = (FlowLayout) mainpanel.getLayout();
		layout.setAlignment(FlowLayout.LEFT);
		JLabel lblIMG = new JLabel("Product Image");
		lblIMG.setVerticalAlignment(SwingConstants.BOTTOM);
		lblIMG.setHorizontalAlignment(SwingConstants.CENTER);
		lblIMG.setPreferredSize(new Dimension(110, 20));
		lblIMG.setBorder(new BevelBorder(BevelBorder.LOWERED, new Color(128,
				128, 128), new Color(128, 128, 128), new Color(192, 192, 192),
				new Color(192, 192, 192)));
		lblIMG.setOpaque(true);
		lblIMG.setBackground(Color.GRAY);
		lblIMG.setForeground(Color.white);
		mainpanel.add(lblIMG);
		mainpanel.add(lblPrice_1);
		mainpanel.add(lblQuantity);
		mainpanel.add(lblQuantity_1);
		mainpanel.add(label);

		final JLabel totalPriceLabel = new JLabel();
		JLabel TotalPrice = new JLabel("Total Price: ");
		JPanel panel_total = new JPanel();
		for (int i = 0; i < getOrderList().size(); i++) {
			final int a = i;
			JPanel panel = new JPanel();
			panel.setLayout(null);
			if (i % 2 == 0) {
				panel.setBackground(Color.WHITE);
			} else {
				panel.setBackground(new Color(240, 240, 240));
			}

			panel.setPreferredSize(new Dimension(730, 90));
			JButton btn_remove = new JButton("remove");
			final JSpinner num = new JSpinner();

			num.setModel(new SpinnerNumberModel(new Integer(1), new Integer(1),
					new Integer(10), new Integer(1)));
			JLabel lblProductName = new JLabel();
			lblProductName.setFont(new Font("Tahoma", Font.PLAIN, 14));
			lblProductName.setText(getOrderList().get(i).getProdName());
			final JLabel lblPrice = new JLabel();
			lblPrice.setText(Double.toString(getOrderList().get(i).getPrice()));
			final double price = getOrderList().get(i).getPrice();
			lblPrice.setFont(new Font("Tahoma", Font.PLAIN, 18));
			JLabel lblimg = new JLabel();
			Image bg = Toolkit
					.getDefaultToolkit()
					.createImage(
							System.getProperty("user.dir")
									+ "/img/product-img/"
									+ getOrderList().get(i).getProdName()
									+ ".png")
					.getScaledInstance(100, 70, Image.SCALE_DEFAULT);
			lblimg.setIcon(new ImageIcon(bg));
			lblimg.setBounds(10, 10, 100, 70);
			lblProductName.setBounds(130, 10, 100, 50);
			lblPrice.setBounds(240, 10, 100, 50);
			num.setBounds(360, 10, 100, 40);
			((DefaultEditor) num.getEditor()).getTextField().setEditable(false);
			num.addChangeListener(new ChangeListener() {
				public void stateChanged(ChangeEvent arg0) {

					double total = price * (Integer) num.getValue();
					total_Price = 0;
					Component[] item = mainpanel.getComponents();
					int panelCount = 0;
					int temp_quantity = 1;
					quantity_item = new ArrayList<Integer>();
					for (int i = 0; i < item.length; i++) {
						if (item[i] instanceof JPanel) {
							JPanel itemPanel = (JPanel) item[i];
							Component[] itemPanelItems = itemPanel
									.getComponents();
							for (int a = 0; a < itemPanelItems.length; a++) {
								if (itemPanelItems[a] instanceof JSpinner) {
									JSpinner quantity = (JSpinner) itemPanelItems[a];
									total_Price += (Integer) quantity
											.getValue()
											* getOrderList().get(panelCount)
													.getPrice();
									temp_quantity = (Integer) quantity
											.getValue();
									quantity_item.add(temp_quantity);
									break;
								}

							}
							panelCount++;
						}

					}

					lblPrice.setText(Double.toString(total));
					totalPriceLabel.setText(Double.toString(total_Price));
					totalPriceLabel.setFont(new Font("Tahoma", Font.PLAIN, 20));
					setTotalPrice(total_Price);
				}

			});

			totalPriceLabel.setFont(new Font("Tahoma", Font.PLAIN, 20));
			btn_remove.setBounds(480, 10, 200, 40);
			btn_remove.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					order_list.remove(a);
					addProducts();
				}
			});
			panel.add(num);
			panel.add(lblimg);
			panel.add(lblProductName);
			panel.add(lblPrice);
			panel.add(btn_remove);

			mainpanel.add(panel);
			panel_total.add(TotalPrice);
			panel_total.add(totalPriceLabel);
			mainpanel.add(panel_total);
		}
		double temp_price = 0;
		for (int b = 0; b < getOrderList().size(); b++) {
			temp_price = temp_price + getOrderList().get(b).getPrice();
			totalPriceLabel.setText(Double.toString(temp_price));
			setTotalPrice(temp_price);
		}

		lblPrice_1.setVerticalAlignment(SwingConstants.BOTTOM);
		lblPrice_1.setPreferredSize(new Dimension(100, 20));
		lblPrice_1.setOpaque(true);
		lblPrice_1.setHorizontalAlignment(SwingConstants.CENTER);
		lblPrice_1.setForeground(Color.WHITE);
		lblPrice_1.setBorder(new BevelBorder(BevelBorder.LOWERED, new Color(
				128, 128, 128), new Color(128, 128, 128), new Color(192, 192,
				192), new Color(192, 192, 192)));
		lblPrice_1.setBackground(Color.GRAY);

		lblQuantity.setVerticalAlignment(SwingConstants.BOTTOM);
		lblQuantity.setPreferredSize(new Dimension(100, 20));
		lblQuantity.setOpaque(true);
		lblQuantity.setHorizontalAlignment(SwingConstants.CENTER);
		lblQuantity.setForeground(Color.WHITE);
		lblQuantity.setBorder(new BevelBorder(BevelBorder.LOWERED, new Color(
				128, 128, 128), new Color(128, 128, 128), new Color(192, 192,
				192), new Color(192, 192, 192)));
		lblQuantity.setBackground(Color.GRAY);

		lblQuantity_1.setVerticalAlignment(SwingConstants.BOTTOM);
		lblQuantity_1.setPreferredSize(new Dimension(150, 20));
		lblQuantity_1.setOpaque(true);
		lblQuantity_1.setHorizontalAlignment(SwingConstants.CENTER);
		lblQuantity_1.setForeground(Color.WHITE);
		lblQuantity_1.setBorder(new BevelBorder(BevelBorder.LOWERED, new Color(
				128, 128, 128), new Color(128, 128, 128), new Color(192, 192,
				192), new Color(192, 192, 192)));
		lblQuantity_1.setBackground(Color.GRAY);

		label.setVerticalAlignment(SwingConstants.BOTTOM);
		label.setPreferredSize(new Dimension(260, 20));
		label.setOpaque(true);
		label.setHorizontalAlignment(SwingConstants.CENTER);
		label.setForeground(Color.WHITE);
		label.setBorder(new BevelBorder(BevelBorder.LOWERED, new Color(128,
				128, 128), new Color(128, 128, 128), new Color(192, 192, 192),
				new Color(192, 192, 192)));
		label.setBackground(Color.GRAY);

		ShoppingCartView.setPanel(mainpanel);

	}

	public void addOrderList(String nric) {
		System.gc();
		JLabel Order_id = new JLabel("Order id");
		JLabel Order_date = new JLabel("Order date");
		JLabel Order_price = new JLabel("Price");
		JLabel label = new JLabel("");
		final JPanel mainpanel = new JPanel();
		mainpanel.setPreferredSize(new Dimension(750,
				getOrderList().size() * 100 + 50));
		FlowLayout layout = (FlowLayout) mainpanel.getLayout();
		layout.setAlignment(FlowLayout.LEFT);

		mainpanel.add(Order_id);
		mainpanel.add(Order_date);
		mainpanel.add(Order_price);
		mainpanel.add(label);
		getOrderedHistory(nric);
		for (int i = 0; i < getOrderList().size(); i++) {
			final int a = i;
			JPanel panel = new JPanel();
			panel.setLayout(null);
			if (i % 2 == 0) {
				panel.setBackground(Color.WHITE);
			} else {
				panel.setBackground(new Color(240, 240, 240));
			}

			panel.setPreferredSize(new Dimension(730, 90));
			JLabel lblID = new JLabel();
			lblID.setFont(new Font("Tahoma", Font.PLAIN, 18));
			// lblID.setText()
			lblID.setBounds(100, 5, 100, 100);
			JLabel lblDate = new JLabel();
			lblDate.setFont(new Font("Tahoma", Font.PLAIN, 18));
			JLabel lblPrice = new JLabel();
			lblPrice.setFont(new Font("Tahoma", Font.PLAIN, 18));

			JButton btn_view = new JButton("View");
		}

		Order_id.setVerticalAlignment(SwingConstants.BOTTOM);
		Order_id.setPreferredSize(new Dimension(100, 20));
		Order_id.setOpaque(true);
		Order_id.setHorizontalAlignment(SwingConstants.CENTER);
		Order_id.setForeground(Color.WHITE);
		Order_id.setBorder(new BevelBorder(BevelBorder.LOWERED, new Color(128,
				128, 128), new Color(128, 128, 128), new Color(192, 192, 192),
				new Color(192, 192, 192)));
		Order_id.setBackground(Color.GRAY);

		Order_date.setVerticalAlignment(SwingConstants.BOTTOM);
		Order_date.setPreferredSize(new Dimension(100, 20));
		Order_date.setOpaque(true);
		Order_date.setHorizontalAlignment(SwingConstants.CENTER);
		Order_date.setForeground(Color.WHITE);
		Order_date.setBorder(new BevelBorder(BevelBorder.LOWERED, new Color(
				128, 128, 128), new Color(128, 128, 128), new Color(192, 192,
				192), new Color(192, 192, 192)));
		Order_price.setBackground(Color.GRAY);

		Order_price.setVerticalAlignment(SwingConstants.BOTTOM);
		Order_price.setPreferredSize(new Dimension(150, 20));
		Order_price.setOpaque(true);
		Order_price.setHorizontalAlignment(SwingConstants.CENTER);
		Order_price.setForeground(Color.WHITE);
		Order_price.setBorder(new BevelBorder(BevelBorder.LOWERED, new Color(
				128, 128, 128), new Color(128, 128, 128), new Color(192, 192,
				192), new Color(192, 192, 192)));
		Order_price.setBackground(Color.GRAY);

		label.setVerticalAlignment(SwingConstants.BOTTOM);
		label.setPreferredSize(new Dimension(260, 20));
		label.setOpaque(true);
		label.setHorizontalAlignment(SwingConstants.CENTER);
		label.setForeground(Color.WHITE);
		label.setBorder(new BevelBorder(BevelBorder.LOWERED, new Color(128,
				128, 128), new Color(128, 128, 128), new Color(192, 192, 192),
				new Color(192, 192, 192)));
		label.setBackground(Color.GRAY);
	}

	

	// create payment
	public int CreatePayment(double d) {
		Payment payment = new Payment();
		payment.setAmount(d);
		return payment.addPayment(payment);
	}

	// get number of row for product view
	public int getRecordNumProduct() {
		return Product.getRecordNum();
	}

	// get unava number of row
	public int getUnavaRecordNumProduct() {
		return Product.getUnavaRecordNum();
	}

	// get number of row for product comment
	public int getRecordNumComment() {
		return Comment.getRecordNum();
	}

	// delete product function
	public boolean delete() {
		boolean isSuccessful = false;
		selectedItems = esview.getSelectedItems();

		for (Object selectedItem : selectedItems) {
			Product _selectedItem = (Product) selectedItem;

			if (_selectedItem.delProduct((Product) selectedItem)) {
				isSuccessful = true;
			} else {
				isSuccessful = false;
				break;
			}
		}

		return isSuccessful;
	}
	public ArrayList<OrderItem> getShoppingList(int id){
		OrderItem item = new OrderItem();
		return item.getSelectedItem(id);
	}
	public Product getProduct(int productID){
		Product product = new Product();
		return product.getSelected(productID);
	}
	// retrieve product function
	public boolean retrieve() {
		boolean isSuccessful = false;
		selectedItems = retireveView.getSelectedItems();

		for (Object selectedItem : selectedItems) {
			Product _selectedItem = (Product) selectedItem;

			if (_selectedItem.retrieveProduct((Product) selectedItem)) {
				isSuccessful = true;
			} else {
				isSuccessful = false;
				break;
			}
		}

		return isSuccessful;
	}

	// delete comment function
	public boolean deleteComment() {
		boolean isSuccessful = false;
		selectedItems = commentView.getSelectedItems();

		for (Object selectedItem : selectedItems) {
			Comment _selectedItem = (Comment) selectedItem;

			if (_selectedItem.delComment((Comment) selectedItem)) {
				isSuccessful = true;
			} else {
				isSuccessful = false;
				break;
			}
		}

		return isSuccessful;
	}

	// product deletion message confirmation
	public boolean showDeleteDialog() {
		boolean response = false;
		int option = JOptionPane.showConfirmDialog(null,
				"Are you sure you want to delete "
						+ esview.getSelectedItems().length + " item(s)?",
				"Deletion", JOptionPane.YES_NO_OPTION);
		switch (option) {
		case JOptionPane.YES_OPTION:
			response = true;
			break;
		case JOptionPane.NO_OPTION:
			response = false;
			break;
		}
		return response;

	}

	// Product retrieve message confirmation

	public boolean showRetrieveDialog() {
		boolean response = false;
		int option = JOptionPane.showConfirmDialog(
				null,
				"Are you sure you want to retrieve "
						+ retireveView.getSelectedItems().length + " item(s)?",
				"Retrieve", JOptionPane.YES_NO_OPTION);
		switch (option) {
		case JOptionPane.YES_OPTION:
			response = true;
			break;
		case JOptionPane.NO_OPTION:
			response = false;
			break;
		}
		return response;

	}

	// get multiple product for shop view
	public ArrayList<Product> getMultiple(int position, int count) {
		return Product.getMultipleItem(position, count);
	}

	// get multiple comment for each product
	public ArrayList<Comment> getMultipleComment(int position, int count,
			String productName) {
		return Comment.getMultipleComment(position, count, productName);
	}

	public ArrayList<Product> getMultipleDeletedItem(int position, int count) {
		return Product.getMultipleDeletedItem(position, count);
	}

	// email sending function
	public boolean sendEmail() {
		boolean valid = false;
		Email email = Email.getInstance();
		if (email.send(ordermail.getFromEmail(), ordermail.getToEmail(),
				ordermail.getOrderID(), ordermail.getMessage()) == true) {
			JOptionPane.showMessageDialog(null,
					"Email had been sent! Please check your Email");
			valid = true;
		} else {
			JOptionPane.showMessageDialog(null,
					"Email not send! Please check you input!");
			valid = false;
		}
		return valid;

	}
	public boolean sendEmailShopping(int OrderID, String orderList) {
		boolean valid = false;
		Email email = Email.getInstance();
		if (email.send("IT2297OOADPROJECT@gmail.com", UserApp.getCurrentUser().getEmail(),"Order ID:" + Integer.toString(OrderID), orderList) == true) {
			JOptionPane.showMessageDialog(null,
					"Email had been sent! Please check your Email");
			valid = true;
		} else {
			JOptionPane.showMessageDialog(null,
					"Email not send! Please check you input!");
			valid = false;
		}
		return valid;

	}
/*
	public boolean sendImgEmail(Image img) {
		boolean valid = false;
		Email email = Email.getInstance();
		if (email.sendImg("IT2297OOADPROJECT@gmail.com", UserApp
				.getCurrentUser().getEmail(), "Sales Chart", img) == true) {
			JOptionPane.showMessageDialog(null,
					"Email had been sent! Please check your Email");
			valid = true;
		} else {
			JOptionPane.showMessageDialog(null,
					"Email not send! Please check you input!");
			valid = false;
		}
		return valid;
	}
*/
	public static String padRight(String s, int n) {
		return String.format("%1$-" + n + "s", s);
	}

	public static String padLeft(String s, int n) {
		return String.format("%1$" + n + "s", s);
	}

	public BufferedImage createImage(JPanel panel) {

		int w = panel.getWidth();
		int h = panel.getHeight();
		BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
		Graphics2D g = bi.createGraphics();
		panel.paint(g);
		return bi;
	}

	public Image bufferedImagetoImage(BufferedImage bi) {
		return Toolkit.getDefaultToolkit().createImage(bi.getSource());
	}

	public void clear() {
		order_list.clear();
		quantity_item.clear();
		pay.clear();
		orderList.clear();
		order_item.clear();
		Product_ordered.clear();
	}

}
